package com.micangcaihang.micang.view.mvp.presenter;

import com.micangcaihang.common.baserx.RxManager;
import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.http.exception.ApiException;
import com.micangcaihang.micang.entity.PickerEntity;
import com.micangcaihang.micang.http.MiSubScriber;
import com.micangcaihang.micang.view.mvp.contract.MatesContract;
import com.micangcaihang.micang.view.mvp.model.MatesModel;

import java.util.List;

import io.reactivex.Observable;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/8/8
 *     update : 2017/8/8
 *     desc   : MiCang
 *     version: 1.0
 * </pre>
 */
public class MatesPresenter extends MatesContract.Presenter<MatesContract.View>{
    private MatesModel mModel;
    private RxManager mRxManager;
    private MatesContract.View mView;
    public MatesPresenter(MatesContract.View view) {
        mModel = new MatesModel();
        mRxManager = new RxManager();
        mView = view;
    }

    public void onDetached() {
        mRxManager.clear();
    }
    /**
     * 加载下拉框数据
     *
     * @param type
     * @param title
     */
    @Override
    public void loadPickerViewData(String type, String title) {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getPickerViewData(type);
        mRxManager.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnPickerViewData(listApiResult.data, title);
                } else {
                    mView.showTip("暂时没有" + title + "信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }
}
